@extends('admin.layout')
@section('title', '网站 - 用户编辑')
@section('content')
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>用户编辑 <small>非必要填写可为空</small></h5>
                </div>
                <div class="ibox-content">
                    <div class="row">
                        <form role="form" id="form" enctype="multipart/form-data">
                            <div class="col-sm-12">
                                <div class="form-group">
                                    <label>用户账号</label>
                                    <input type="text" placeholder="{{$data->user_name}}" name="name" value="{{$data->user_name}}" class="form-control" readonly="readonly">
                                </div>
                                <div class="form-group">
                                    <label>用户昵称</label>
                                    <input type="text" placeholder="请输入用户昵称" name="nick" value="{{$data->nickname}}" class="form-control">
                                </div>
                                <div class="form-group">
                                    <label>用户邮箱</label>
                                    <input type="text" placeholder="请输入用户邮箱" name="email" value="{{$data->email}}" class="form-control">
                                </div>
                               
                                <div class="form-group">
                                    <label>用户头像</label>
                                    <input class="form-control-file" name="file" id="fileicon" type="file" aria-describedby="fileHelp">
                                </div>
                                
                                <div class="form-group">
                                    <img src="{{$data->icon}}" id="icom" height="50px" />
                                    <input type="hidden" name="icon"id="icon" value="{{$data->icon}}">
                                </div>
                                 <div class="form-group">
                                    <input type="hidden" name="id" value="{{$data->id}}">
                                </div>
                                <div>
                                    <button class="btn btn-sm btn-info pull-right m-t-n-xs" type="button" id="hold"><strong>立即保存</strong>
                                    </button>
                                </div>
                            </div>
                        
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
        <script>
            window.onload=function(){
                var input = document.querySelector('input[type=file]');
                input.onchange=function(e){
                    if ($('#fileicon').val()!='') {
                        fd = new FormData(document.getElementById('form'));
                        $.ajax({
                            url: 'upimg',
                            type: 'POST',
                            timeout: 3000,
                            dataType: 'JSON',
                            contentType: 'application/x-www-form-urlencoded;charset=utf-8',
                            data: fd,
                            contentType: false,//不设置内容类型
                            processData: false,//不处理数据
                            success: function (data) {
                                if (data.code == 200) {
                                    toastr.success(data.msg);//提示
                                    document.getElementById('icon').value = data.data.url;//地址
                                    // document.getElementById('fileicon').value = '1';//清空
                                    document.getElementById('icom').src = data.data.url;//地址
                                } else {
                                    toastr.warning(data.msg);
                                }
                            },
                            error: function (XMLResponse) {
                                //异常捕获
                                toastr.error(XMLResponse.responseText);
                            }
                        });
                    }
                }
            }
            
            $('#hold').click(function () {
                fd = new FormData(document.getElementById('form'));
                $.ajax({
                    url: 'userinfo',
                    type: 'POST',
                    timeout: 3000,
                    dataType: 'JSON',
                    contentType: 'application/x-www-form-urlencoded;charset=utf-8',
                    data: fd,
                    contentType: false,//不设置内容类型
                    processData: false,//不处理数据
                    success: function (data) {
                        if (data.code == 200) {
                            // toastr.success(data.msg);
                            layer.msg(data.msg, {
                                // icon: 1,
                                time: 1000 //2秒关闭（如果不配置，默认是3秒）
                                }, function(){
                                    parent.location.reload();//更新父级页面
                            });   
                        } else {
                            toastr.warning(data.msg);
                        }
                    },
                    error: function (XMLResponse) {
                        //异常捕获
                        toastr.error(XMLResponse.responseText);
                    }
                });
            });
            
        </script>
@endsection